﻿using MediatR;
using Product.API.Applcation.Command.ProductType;
using Product.Infrstructre.Impl;

namespace Product.API.Applcation.CommandHandlers.ProductType
{
    /// <summary>
    /// 产品分类删除
    /// </summary>
    public class ProductDeleteCommandHandler : IRequestHandler<ProductDeleteCommand, int>
    {
        protected readonly IbaseProductRepository ibaseProductRepository;

        public ProductDeleteCommandHandler(IbaseProductRepository ibaseProductRepository)
        {
            this.ibaseProductRepository = ibaseProductRepository;
        }

        public async Task<int> Handle(ProductDeleteCommand request, CancellationToken cancellationToken)
        {
            var code = 0;
            foreach (var item in request.ProductTypeID)
            {
                var list = (await ibaseProductRepository.GetAllAsync()).FirstOrDefault(x => x.ProductTypeID == item);
                if (list != null)
                {
                    list.IsDel = true;
                    list.DelName = request.U_Name;
                    list.DelTime = DateTime.Now;
                    await ibaseProductRepository.UpdateAsync(list);
                    code = 1;
                }
                else
                {
                    code = -1;
                }
            }
            return code;
        }
    }
}
